package algorithms.sort;

import java.util.*;

/**
 * 1、算法基础
 * 一年的全国高考考生人数为800万(100个Text文件 ，文件内容格式：身份证ID,总分数)，
 * 分数使用标准分，最低100分 ，最高900分 ，没有小数，请打印出来考分为800总人数，
 * 考分为200的考生排位范围(相同分数，随机向后排,高分先排)。
 */
public class CountSort2 {
    public static void main(String[] args) {
        Student[] students = {
                new Student(UUID.randomUUID().toString(), 100),
                new Student(UUID.randomUUID().toString(), 100),
                new Student(UUID.randomUUID().toString(), 100),
                new Student(UUID.randomUUID().toString(), 100),
                new Student(UUID.randomUUID().toString(), 900),
                new Student(UUID.randomUUID().toString(), 900),
                new Student(UUID.randomUUID().toString(), 800),
                new Student(UUID.randomUUID().toString(), 800),
                new Student(UUID.randomUUID().toString(), 800),
                new Student(UUID.randomUUID().toString(), 101),
                new Student(UUID.randomUUID().toString(), 101),
                new Student(UUID.randomUUID().toString(), 200),
                new Student(UUID.randomUUID().toString(), 200),
                new Student(UUID.randomUUID().toString(), 200),
                new Student(UUID.randomUUID().toString(), 200),
                new Student(UUID.randomUUID().toString(), 200),
                new Student(UUID.randomUUID().toString(), 122),
                new Student(UUID.randomUUID().toString(), 122)
        };

        List<Student> sort = countSort(Arrays.asList(students), 900, 100);

        sort.stream().forEach(System.out::println);
    }


    /**
     * 计数排序
     *
     * @param students
     * @param max : 最大分数
     * @param min : 最小分数
     * @return
     */
    public static List<Student> countSort(List<Student> students, int max, int min) {
        if (Objects.isNull(students) || students.isEmpty())
            return new ArrayList<>();
        // 分数端的学生 key:分数，vale:该分数段的学生
        HashMap<Integer, List<Student>> scoreMap = new HashMap<>();

        //  1.先跑一遍数组统计每个分数段的人数；
        students.stream().forEach(student -> {
            List<Student> scores = scoreMap.get(student.getScore());
            if (Objects.isNull(scores)) {
                scores = new ArrayList<>();
            }
            scores.add(student);
            scoreMap.put(student.getScore(), scores);
        });

        // 2.根据词频表给原数组赋值，完成排序。
        List<Student> sort = new ArrayList<>();
        for (int i = min; i <= max; i++) {
            List<Student> list = scoreMap.get(i);
            if (Objects.isNull(list) || list.isEmpty()) {
                continue;
            }

            // 题要求结果 start
            if (i == 800) {
                System.out.println("分为800总人数:"+list.size());
            }
            if(i == 200) {
                System.out.println("分为200的详情:");
                list.stream().forEach(System.out::println);
            }
            // 题要求结果 end

            sort.addAll(list);
        }
        scoreMap.clear();
        return sort;
    }

}

class Student {
    // 身份证
    private String idCard;
    // 分数
    private int score;

    public Student(String idCard, int score) {
        this.idCard = idCard;
        this.score = score;
    }

    public String getIdCard() {
        return idCard;
    }

    public int getScore() {
        return score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "idCard='" + idCard + '\'' +
                ", score=" + score +
                '}';
    }
}
